//
//  CALAddEatingViewController.m
//  CalCounter
//
//  Created by Toanpro on 9/7/13.
//  Copyright (c) 2013 Le thanh Lam. All rights reserved.
//

#import "CALAddEatingViewController.h"
#import "NSDate+Ex.h"
#import "DataHelper.h"
#import "DateTimeView.h"
@interface CALAddEatingViewController (){
    NSDate *date;
    __weak IBOutlet UITextField *eatingNameTexfield;
    __weak IBOutlet UIButton *timeButton;
    __weak IBOutlet UIDatePicker *datePicker;
}

@end

@implementation CALAddEatingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelTapped:)];
    self.navigationItem.leftBarButtonItem = leftBtn;
    
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveTapped:)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    
    
    if(self.eatingSelected){        //eatingSelected != nil ==> edit mode, else is add mode
        self.title = NSLocalizedString(@"edit_eating", nil);
        eatingNameTexfield.text = self.eatingSelected.eatingName;
        
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"HH:mm"];
        date = [df dateFromString:self.eatingSelected.eatingTime];
        
        [timeButton setTitle:[NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"time", nil),self.eatingSelected.eatingTime] forState:UIControlStateNormal];
        [datePicker setDate:date animated:NO];
    }else{
        self.title = NSLocalizedString(@"add_eating", nil);
        eatingNameTexfield.text = [NSString stringWithFormat:@"%@ %d",NSLocalizedString(@"eating", nil) ,self.eatingCount + 1];
        date = [NSDate date];
        [timeButton setTitle:[NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"time", nil),[date timeString]] forState:UIControlStateNormal];
    }
    
    
    datePicker.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height + 216/2);
    // Do any additional setup after loading the view from its nib.
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if(self.eatingSelected){
        [eatingNameTexfield becomeFirstResponder];
    }
}

- (void)cancelTapped:(id)sender{
    NSLog(@"cancel tapped");
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)saveTapped:(id)sender{
    if([eatingNameTexfield.text length] == 0)
        return;
    
    if(self.eatingSelected){
        self.eatingSelected.eatingName = eatingNameTexfield.text;
        self.eatingSelected.eatingTime = [date timeString];
        [[DataHelper sharedDatabase] updateExistEating:self.eatingSelected];
    }else{
        [[DataHelper sharedDatabase] addEatingWithName:eatingNameTexfield.text time:[date timeString] date:[[DateTimeView selectDate] dateString]];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    NSLog(@"save tapped");
}

- (IBAction)timeButtonTapped:(id)sender {
    [eatingNameTexfield resignFirstResponder];
    [UIView animateWithDuration:0.5f animations:^{
        CGRect vr = self.view.frame;
        datePicker.center = CGPointMake(vr.size.width/2, vr.size.height - 216/2);
    }];
}
- (IBAction)changeTime:(UIDatePicker *)sender {
    date = sender.date;
    [timeButton setTitle:[NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"time", nil),[sender.date timeString]] forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark -  UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    [UIView animateWithDuration:0.5f animations:^{
        datePicker.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height + 216/2);
    }];
//    [textField becomeFirstResponder];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark -
@end
